package com.pengpeng.sheepfoldmall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pengpeng.common.to.IsHasStockTo;
import com.pengpeng.common.utils.R;
import com.pengpeng.sheepfoldmall.ware.feign.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pengpeng.common.utils.PageUtils;
import com.pengpeng.common.utils.Query;

import com.pengpeng.sheepfoldmall.ware.dao.WareSkuDao;
import com.pengpeng.sheepfoldmall.ware.entity.WareSkuEntity;
import com.pengpeng.sheepfoldmall.ware.service.WareSkuService;
import org.springframework.util.StringUtils;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<WareSkuEntity> queryWrapper = new LambdaQueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)){
            queryWrapper.eq(WareSkuEntity::getSkuId,skuId);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)){
            queryWrapper.eq(WareSkuEntity::getWareId,wareId);
        }

        IPage<WareSkuEntity> page = this.page(new Query<WareSkuEntity>().getPage(params),queryWrapper);

        return new PageUtils(page);
    }

    /**
     * 将采购成功采购项的进行入库
     * @param skuId
     * @param wareId
     * @param skuNum
     */
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //先判断对应仓库的对应商品存不存在
        List<WareSkuEntity> wareSkuEntities = this.list(new LambdaQueryWrapper<WareSkuEntity>()
                .eq(WareSkuEntity::getSkuId, skuId).eq(WareSkuEntity::getWareId, wareId));
        //若不存在则新增库存
        if (wareSkuEntities == null || wareSkuEntities.size() == 0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStockLocked(0);
            wareSkuEntity.setStock(skuNum);

            //远程调用商品服务去获取商品名称
            try {
                R info = productFeignClient.info(skuId);
                if (info.getCode() == 0){
                    Map<Object,String> skuInfo = (Map<Object, String>) info.get("skuInfo");
                    wareSkuEntity.setSkuName(skuInfo.get("skuName"));
                }
            } catch (Exception e) {

            }
            this.save(wareSkuEntity);
        }
        //若存在则更新库存（新增库存量）
        baseMapper.addStock(skuId,wareId,skuNum);
    }

    /**
     * 查询对应商品的不同sku是否有库存(供商品系统--上架功能 使用)
     */
    @Override
    public List<IsHasStockTo> isHasStockBySkuIds(List<Long> skuIds) {

        //先查询对应sku的库存数(表中库存减去锁定库存)
        List<WareSkuEntity> wareSkuEntities =  baseMapper.qryIsHasStock(skuIds);

        //复制to对象
        List<IsHasStockTo> isHasStockTos = wareSkuEntities.stream().map(it -> {
            IsHasStockTo isHasStockTo = new IsHasStockTo();
            isHasStockTo.setSkuId(it.getSkuId());
            isHasStockTo.setIsStock(it.getStock() > 0);
            return isHasStockTo;
        }).collect(Collectors.toList());

        return isHasStockTos;
    }

}